package com.tencent.qcloud.tim.uikit.modules.group.member

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.Constants
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.StringUtil
import com.tencent.qcloud.tim.uikit.R
import com.tencent.qcloud.tim.uikit.bean.*
import kotlinx.android.synthetic.main.dialog_friend_search_h.view.*

/**
 * 好友搜索


 * @date  2020/4/23  16:47
 * @version 1.0
 */

class InviteSearchPopup(context: Context, val mType: Int) : FullScreenPopupView(context) {

    private var mAdapter: MAdapter? = null

    companion object {
        //mType = 搜索类型 0 全部，1 互粉好友，2 直推好友
        fun load(context: Context, type: Int = FRIEND_TYPE_ALL) {
            XPopup.Builder(context)
                    .hasStatusBar(true)
                    .asCustom(InviteSearchPopup(context, type))
                    .show()
        }
    }


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_friend_search_h
    }


    override fun onDismiss() {
        super.onDismiss()
    }


    override fun onCreate() {
        super.onCreate()
        etSearch.addTextChangedListener(editListener)
        mAdapter = MAdapter(R.layout.item_mine_friend_h, mutableListOf(), mType)
        mRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mRecycler.adapter = mAdapter
        mAdapter?.setOnItemChildClickListener(itemChildClickListener)
        tvDismiss.setOnClickListener { dismiss() }
    }


    private val itemChildClickListener = OnItemChildClickListener { adapter, view, position ->
        when (view.id) {
            R.id.tvButton -> {
                if (ClickUtil.canClick()) {
                    val item = InviteLiveData.getPos(position, mType)
                    if (item?.isEdit == true) {
                        InviteLiveData.changeCheckStatus(position, mType)
                        mAdapter?.notifyItemChanged(position, Constants.PAYLOAD)
                    }
                }

            }
            R.id.tvRemark -> {
                val item = InviteLiveData.friendList.value?.get(position)
                item?.run {
                    if (isEdit) {
                        // doRemark
                    }
                }
            }

        }

    }

    private fun doSearch(key: String) {
        mAdapter?.setNewInstance(InviteLiveData.doSearch(key, mType))
        mAdapter?.notifyDataSetChanged()
    }


    private val editListener = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            s?.run {
                doSearch(this.toString())
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }


    private class MAdapter(layout: Int, data: MutableList<MsgFriendBean>?, val curType: Int)
        : BaseQuickAdapter<MsgFriendBean, BaseViewHolder>(layout, data) {

        init {
            addChildClickViewIds(R.id.tvButton, R.id.tvRemark)
        }

        override fun convert(holder: BaseViewHolder, item: MsgFriendBean) {
            item.run {
                Glide.with(holder.getView<ImageView>(R.id.ivImage))
                        .load(avatarThumb)
                        .circleCrop()
                        .into(holder.getView(R.id.ivImage))
                holder.setText(R.id.tvName, userNickname)
                        .setText(R.id.tvCell, "手机号：$serialNo")

                holder.setGone(R.id.tvButton, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) false else !isEdit)
                holder.setGone(R.id.tvRemark, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) true else !isEdit)
                holder.setGone(R.id.tvGoMsg, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) true else isEdit)
                holder.getView<ImageView>(R.id.tvButton).isSelected = isChecked

            }

        }


        override fun convert(holder: BaseViewHolder, item: MsgFriendBean, payloads: List<Any>) {
            if (StringUtil.isListNotEmpty(payloads)) {
                item.run {
                    holder.setGone(R.id.tvButton, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) false else !isEdit)
                    holder.setGone(R.id.tvRemark, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) true else !isEdit)
                    holder.setGone(R.id.tvGoMsg, if (curType == FRIEND_TYPE_FANS || curType == FRIEND_TYPE_SHARE) true else isEdit)
                    holder.getView<ImageView>(R.id.tvButton).isSelected = item.isChecked
                }
            }
        }


    }


}